package cn.tedu.tea.service;

import cn.tedu.tea.domain.Goods;
import cn.tedu.tea.domain.param.GoodsAddNewDTO;
import cn.tedu.tea.domain.param.GoodsUpdateDTO;
import cn.tedu.tea.domain.vo.GoodsListVO;
import cn.tedu.tea.domain.vo.GoodsStandardVO;
import cn.tedu.tea.ex.ServiceException;
import cn.tedu.tea.mapper.GoodsMapper;
import cn.tedu.tea.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class GoodsServiceImpl implements IGoodsService {

    @Autowired
    private GoodsMapper goodsMapper;

    @Override
    public void addNew(GoodsAddNewDTO goodsAddNewDTO) {
        log.debug("开始处理【添加货物】的业务，参数： {}", goodsAddNewDTO);
        int count = goodsMapper.countByTitle(goodsAddNewDTO.getTitle());
        if (count > 0){
            String message = "添加货物失败，系统文件名已被占用！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_CONFLICT, message);
        }
        Goods goods = new Goods();
        BeanUtils.copyProperties(goodsAddNewDTO, goods);
        goods.setModifiedUserId(0).setModifiedUserName("");
        int rows = goodsMapper.insert(goods);
        if (rows != 1){
            String message = "添加货物失败，服务器忙，请稍后再试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_INSERT, message);
        }
    }

    @Override
    public void deleteById(Integer id) {
        log.debug("开始处理【根据id删除货物】的业务，参数： {}", id);
        GoodsStandardVO goodsStandard = goodsMapper.getStandardById(id);
        if (goodsStandard == null){
            String message = "删除货物失败，要删除的货物不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }
        int rows = goodsMapper.deleteById(id);
        if (rows != 1){
            String message = "删除货物失败，服务器忙，请稍后再试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_DELETE, message);
        }
    }

    @Override
    public void deleteByIds(Integer[] ids) {
        log.debug("开始处理【根据id批量删除货物】的业务，参数： {}", ids);
        int rows = goodsMapper.deleteByIds(ids);
        if (rows < 1){
            String message = "删除货物失败，服务器忙，请稍后再试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_DELETE, message);
        }
    }

    @Override
    public void updateInfoById(GoodsUpdateDTO goodsUpdateDTO) {
        log.debug("开始处理【修改货物信息】的业务，参数：{}", goodsUpdateDTO);
        GoodsStandardVO goodsStandard = goodsMapper.getStandardById(goodsUpdateDTO.getId());
        if (goodsStandard == null){
            String message = "修改货物失败，要访问的数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }
        int count = goodsMapper.countByTitleAndNotId(goodsUpdateDTO.getTitle(), goodsUpdateDTO.getId());
        if (count > 0){
            String message = "修改货物失败，系统文件名已被占用！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_CONFLICT, message);
        }
        Goods goods = new Goods();
        BeanUtils.copyProperties(goodsUpdateDTO, goods);
        int rows = goodsMapper.update(goods);
        if (rows != 1){
            String message = "修改货物失败，服务器忙，请稍后再试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_UPDATE, message);
        }
    }

    @Override
    public void setDisplay(Integer id) {
        log.debug("开始处理【将货物设置为上架状态】的业务，参数：{}", id);
        updateStatusById(id, 0);
    }

    @Override
    public void setHidden(Integer id) {
        log.debug("开始处理【将货物设置为下架状态】的业务，参数：{}", id);
        updateStatusById(id, 1);
    }

    @Override
    public GoodsStandardVO getStandardById(Integer id) {
        log.debug("开始处理【根据id查询货物详情】的业务，参数： {}", id);
        GoodsStandardVO goodsStandard = goodsMapper.getStandardById(id);
        if (goodsStandard == null){
            String message = "查询货物详情失败，要查询的数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }
        return goodsStandard;
    }

    @Override
    public List<GoodsListVO> list() {
        log.debug("开始处理【查询货物列表】的业务，无参数");
        List<GoodsListVO> list = goodsMapper.list();
        return list;
    }

    private void updateStatusById(Integer id, Integer status) {
        GoodsStandardVO currentGoods = goodsMapper.getStandardById(id);
        if (currentGoods == null) {
            String message = STATUS_TEXT[status] + "货物失败，货物数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }
        if (currentGoods.getStatus() == status) {
            String message = STATUS_TEXT[status] + "货物失败，此货物已经处于" + STATUS_TEXT[status] + "状态！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_CONFLICT, message);
        }
        Goods updateGoods = new Goods();
        updateGoods.setId(id);
        updateGoods.setStatus(status);
        int rows = goodsMapper.update(updateGoods);
        if (rows != 1) {
            String message = STATUS_TEXT[status] + "货物失败，服务器忙，请稍后再尝试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_UPDATE, message);
        }
    }
}
